package com.ft.oa.service.dev.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.conditions.update.LambdaUpdateChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ft.oa.domain.dev.DevTask;
import com.ft.oa.domain.dev.DevTaskItem;
import com.ft.oa.domain.dev.dto.DevAttachmentDto;
import com.ft.oa.domain.dev.dto.DevTaskDto;
import com.ft.oa.domain.development.DevAllocate;
import com.ft.oa.domain.development.DevAllocateStaff;
import com.ft.oa.domain.dto.ApiResult;
import com.ft.oa.domain.dto.knowledgeBase.AttachmentDto;
import com.ft.oa.domain.sys.SysUser;
import com.ft.oa.enums.DirectoryTypeEnum;
import com.ft.oa.enums.ManagerEnum;
import com.ft.oa.mapper.dev.DevTaskItemMapper;
import com.ft.oa.mapper.dev.DevTaskMapper;
import com.ft.oa.mapper.development.DevAllocateMapper;
import com.ft.oa.mapper.development.DevAllocateStaffMapper;
import com.ft.oa.mapper.sys.SysUserMapper;
import com.ft.oa.service.dev.DevTaskService;
import com.ft.oa.service.knowledgeBase.DirectoryService;
import com.ft.oa.utils.SecurityUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Author: yihui
 * Date: 2022/1/18 10:15 AM
 * FileName: DevTaskServiceImpl
 * Description: 研发任务
 */
@Transactional
@Service
public class DevTaskServiceImpl implements DevTaskService {

    @Resource
    DevTaskMapper devTaskMapper;

    @Resource
    DevTaskItemMapper devTaskItemMapper;

    @Resource
    DevAllocateMapper devAllocateMapper;

    @Resource
    DevAllocateStaffMapper devAllocateStaffMapper;

    @Resource
    SysUserMapper sysUserMapper;

    @Resource
    DirectoryService directoryService;


    @Override
    public ApiResult add(DevTask devTask) {
        Long devAllocateId = devTask.getDevAllocateId();
        DevAllocate devAllocate = devAllocateMapper.selectById(devAllocateId);
        //审核通过的项目才能创建任务 审核状态 0-审核中  1-通过  2-驳回
        if(  1 != devAllocate.getApprovalStatus()){
            return ApiResult.builder().code(30001).msg("审批未通过，无法创建研发任务").build();
        }
        devTask.setDevAllocateName(devAllocate.getDevAllocateName());
        Long userId = devTask.getPrincipalStaffId();
        SysUser user = sysUserMapper.selectById(userId);
        devTask.setPrincipalStaffName(user.getNickname());
        devTask.setCreateDate(LocalDate.now());
        devTask.setCreateDateTime(LocalDateTime.now());
        devTask.setTaskStatus(0);
        devTask.setProgressRate(0);
        Long createStaffId = SecurityUtils.getCurrentUser().getId();
        devTask.setCreateStaffId(createStaffId);
        devTaskMapper.insert(devTask);
        return ApiResult.simpleSuccess();
    }

    @Override
    public ApiResult del(DevTask devTask) {
        LocalDateTime nowDate = LocalDateTime.now();
        LambdaUpdateChainWrapper<DevTaskItem> wrapperItem = new LambdaUpdateChainWrapper<>(devTaskItemMapper);
        boolean updateItem = wrapperItem.eq(DevTaskItem::getDevTaskId, devTask.getId())
                .set(DevTaskItem::getDel, true)
                .set(DevTaskItem::getDelTime, nowDate)
                .update();

        LambdaUpdateChainWrapper<DevTask> wrapper = new LambdaUpdateChainWrapper<>(devTaskMapper);
        boolean update = wrapper.eq(DevTask::getId, devTask.getId())
                .set(DevTask::getDel, true)
                .set(DevTask::getDelTime, nowDate)
                .update();
        return ApiResult.simpleSuccess();
    }

    @Override
    public Page<DevTask> page(DevTask condition) {
        Page<DevTask> pages = new Page<>(condition.getPage(), condition.getSize());
        QueryWrapper<DevTask> wrapper = new QueryWrapper<>();
        wrapper.lambda()
                .eq(DevTask::getDel,false)
                .eq(ManagerEnum.GENERAL.toString().equals(SecurityUtils.getCurrentUser().getManager()), DevTask::getCreateStaffId,SecurityUtils.getCurrentUser().getId())
                .ge(StringUtils.hasText(condition.getCreateStartDate()),DevTask::getCreateDate,condition.getCreateStartDate())
                .le(StringUtils.hasText(condition.getCreateEndDate()),DevTask::getCreateDate,condition.getCreateEndDate())
                .ge(StringUtils.hasText(condition.getPlanFinishStartDate()),DevTask::getPlanFinishDate,condition.getPlanFinishStartDate())
                .le(StringUtils.hasText(condition.getPlanFinishEndDate()),DevTask::getPlanFinishDate,condition.getPlanFinishEndDate())
                .ge(StringUtils.hasText(condition.getActualFinishStartDate()),DevTask::getActualFinishDate,condition.getActualFinishStartDate())
                .le(StringUtils.hasText(condition.getActualFinishEndDate()),DevTask::getActualFinishDate,condition.getActualFinishEndDate())
                .eq(null != condition.getDevAllocateId() ,DevTask::getDevAllocateId, condition.getDevAllocateId())
                .like(StringUtils.hasText(condition.getPrincipalStaffName()) ,DevTask::getPrincipalStaffName, condition.getPrincipalStaffName());

                wrapper.select().orderByDesc("create_date_time");
        Page<DevTask> result = devTaskMapper.selectPage(pages, wrapper);
        return result;
    }

    @Override
    public ApiResult queryStaff(DevAllocateStaff devAllocateStaff) {
        Map queryMap = new HashMap<>();
        queryMap.put("dev_allocate_id",devAllocateStaff.getDevAllocateId());
        List<DevAllocateStaff> devAllocateStaffList = devAllocateStaffMapper.selectByMap(queryMap);
        return ApiResult.simpleSuccess().setData(devAllocateStaffList);
    }

    @Override
    public ApiResult view(DevTask devTask) {
        DevTaskDto devTaskDto = new DevTaskDto();
        DevTask devTask1 = devTaskMapper.selectById(devTask.getId());
        devTaskDto.setDevTask(devTask1);
        Map queryMap  = new HashMap();
        queryMap.put("dev_task_id",devTask.getId());
        List<DevTaskItem> devTaskItemList = devTaskItemMapper.selectByMap(queryMap);
        devTaskDto.setDevTaskItemList(devTaskItemList);
        return ApiResult.simpleSuccess().setData(devTaskDto);
    }

    @Override
    public ApiResult queryDevAllocate() {
        List<Map<Long,String>> devAllocateList = devTaskMapper.queryDevAllocate();
        return ApiResult.simpleSuccess().setData(devAllocateList);
    }

    @Override
    public ApiResult saveAttachment(DevAttachmentDto devAttachmentDto) {
        List<AttachmentDto> attachmentDtos = devAttachmentDto.getAttachments();
        Long devTaskId = devAttachmentDto.getDevTaskId();
        String[]  projectNumbers = devTaskMapper.queryProjectNumbers(devAttachmentDto.getDevAllocateId());
        directoryService.saveProAttachment(attachmentDtos, DirectoryTypeEnum.DEV_TASK.getCode(),String.valueOf(devTaskId),"",projectNumbers);
        return ApiResult.simpleSuccess();
    }


}
